home *** CD-ROM | disk | FTP | other *** search
/ TeX 1995 July / TeX CD-ROM July 1995 (Disc 1)(Walnut Creek)(1995).ISO / graphics / tiff / tools / sgi2tiff.c < prev    next >
C/C++ Source or Header  |  1992-02-10  |  6KB  |  242 lines

  1. #ifndef lint
  2. static char rcsid[] = "$Header: /usr/people/sam/tiff/tools/RCS/sgi2tiff.c,v 1.7 92/02/10 19:04:05 sam Exp $";
  3. #endif
  4.  
  5. /*
  6.  * Copyright (c) 1991, 1992 Sam Leffler
  7.  * Copyright (c) 1991, 1992 Silicon Graphics, Inc.
  8.  *
  9.  * Permission to use, copy, modify, distribute, and sell this software and 
  10.  * its documentation for any purpose is hereby granted without fee, provided
  11.  * that (i) the above copyright notices and this permission notice appear in
  12.  * all copies of the software and related documentation, and (ii) the names of
  13.  * Sam Leffler and Silicon Graphics may not be used in any advertising or
  14.  * publicity relating to the software without the specific, prior written
  15.  * permission of Sam Leffler and Silicon Graphics.
  16.  * 
  17.  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
  18.  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
  19.  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
  20.  * 
  21.  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
  22.  * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
  23.  * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  24.  * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
  25.  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
  26.  * OF THIS SOFTWARE.
  27.  */
  28.  
  29. #include <stdio.h>
  30. #include <gl/image.h>
  31. #include "tiffio.h"
  32.  
  33. typedef    unsigned char u_char;
  34. typedef    unsigned short u_short;
  35. typedef    unsigned long u_long;
  36.  
  37. #define    streq(a,b)    (strcmp(a,b) == 0)
  38.  
  39. static    short config = PLANARCONFIG_CONTIG;
  40. static    u_short compression = COMPRESSION_LZW;
  41. static    u_short predictor = 0;
  42. static    u_short fillorder = 0;
  43. static    long rowsperstrip = -1;
  44.  
  45. static    int tiffcp();
  46. static    void usage();
  47. static    int cpContig();
  48. static    int cpSeparate();
  49.  
  50. main(argc, argv)
  51.     char *argv[];
  52. {
  53.     IMAGE *in;
  54.     TIFF *out;
  55.  
  56.     argc--, argv++;
  57.     if (argc < 2)
  58.         usage();
  59.     for (; argc > 2 && argv[0][0] == '-'; argc--, argv++) {
  60.         if (streq(argv[0], "-none")) {
  61.             compression = COMPRESSION_NONE;
  62.             continue;
  63.         }
  64.         if (streq(argv[0], "-packbits")) {
  65.             compression = COMPRESSION_PACKBITS;
  66.             continue;
  67.         }
  68.         if (streq(argv[0], "-lzw")) {
  69.             compression = COMPRESSION_LZW;
  70.             continue;
  71.         }
  72.         if (streq(argv[0], "-contig")) {
  73.             config = PLANARCONFIG_CONTIG;
  74.             continue;
  75.         }
  76.         if (streq(argv[0], "-separate")) {
  77.             config = PLANARCONFIG_SEPARATE;
  78.             continue;
  79.         }
  80.         if (streq(argv[0], "-lsb2msb")) {
  81.             fillorder = FILLORDER_LSB2MSB;
  82.             continue;
  83.         }
  84.         if (streq(argv[0], "-msb2lsb")) {
  85.             fillorder = FILLORDER_MSB2LSB;
  86.             continue;
  87.         }
  88.         if (streq(argv[0], "-rowsperstrip")) {
  89.             argc--, argv++;
  90.             rowsperstrip = atoi(argv[0]);
  91.             continue;
  92.         }
  93.         if (streq(argv[0], "-predictor")) {
  94.             argc--, argv++;
  95.             predictor = atoi(argv[0]);
  96.             continue;
  97.         }
  98.         usage();
  99.     }
  100.     in = iopen(argv[0], "r");
  101.     if (in == NULL)
  102.         exit(-1);
  103.     out = TIFFOpen(argv[1], "w");
  104.     if (out == NULL)
  105.         exit(-2);
  106.     TIFFSetField(out, TIFFTAG_IMAGEWIDTH, in->xsize);
  107.     TIFFSetField(out, TIFFTAG_IMAGELENGTH, in->ysize);
  108.     TIFFSetField(out, TIFFTAG_BITSPERSAMPLE, 8);
  109.     TIFFSetField(out, TIFFTAG_COMPRESSION, compression);
  110.     if (predictor != 0)
  111.         TIFFSetField(out, TIFFTAG_PREDICTOR, predictor);
  112.     TIFFSetField(out, TIFFTAG_PHOTOMETRIC,
  113.         in->zsize == 1 ? PHOTOMETRIC_MINISBLACK : PHOTOMETRIC_RGB);
  114.     if (fillorder != 0)
  115.         TIFFSetField(out, TIFFTAG_FILLORDER, fillorder);
  116.     TIFFSetField(out, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
  117.     TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, in->zsize);
  118.     TIFFSetField(out, TIFFTAG_MINSAMPLEVALUE, (short) in->min);
  119.     TIFFSetField(out, TIFFTAG_MAXSAMPLEVALUE, (short) in->max);
  120.     TIFFSetField(out, TIFFTAG_PLANARCONFIG, config);
  121.     if (config != PLANARCONFIG_SEPARATE) {
  122.         if (rowsperstrip <= 0)
  123.             rowsperstrip = (8*1024)/TIFFScanlineSize(out);
  124.         TIFFSetField(out, TIFFTAG_ROWSPERSTRIP,
  125.             rowsperstrip == 0 ? 1L : rowsperstrip);
  126.     } else            /* force 1 row/strip for library limitation */
  127.         TIFFSetField(out, TIFFTAG_ROWSPERSTRIP, 1L);
  128.     if (in->name[0] != '\0')
  129.         TIFFSetField(out, TIFFTAG_IMAGEDESCRIPTION, in->name);
  130.     if (config == PLANARCONFIG_CONTIG)
  131.         cpContig(in, out);
  132.     else
  133.         cpSeparate(in, out);
  134.     (void) iclose(in);
  135.     (void) TIFFClose(out);
  136.     exit(0);
  137. bad:
  138.     unlink(argv[1]);
  139.     exit(1);
  140. }
  141.  
  142. static int
  143. cpContig(in, out)
  144.     IMAGE *in;
  145.     TIFF *out;
  146. {
  147.     u_char *buf = (u_char *)malloc(TIFFScanlineSize(out));
  148.     short *r = NULL;
  149.     int x, y;
  150.  
  151.     if (in->zsize == 3) {
  152.         short *g, *b;
  153.  
  154.         r = (short *)malloc(3 * in->xsize * sizeof (short));
  155.         g = r + in->xsize;
  156.         b = g + in->xsize;
  157.         for (y = in->ysize-1; y >= 0; y--) {
  158.             u_char* pp = buf;
  159.  
  160.             getrow(in, r, y, 0);
  161.             getrow(in, g, y, 1);
  162.             getrow(in, b, y, 2);
  163.             for (x = 0; x < in->xsize; x++) {
  164.                 *pp++ = r[x];
  165.                 *pp++ = g[x];
  166.                 *pp++ = b[x];
  167.             }
  168.             if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
  169.                 goto bad;
  170.         }
  171.     } else {
  172.         r = (short *)malloc(in->xsize * sizeof (short));
  173.         for (y = in->ysize-1; y >= 0; y--) {
  174.             getrow(in, r, y, 0);
  175.             for (x = 0; x < in->xsize; x++)
  176.                 buf[x] = r[x];
  177.             if (TIFFWriteScanline(out, buf, in->ysize-y-1, 0) < 0)
  178.                 goto bad;
  179.         }
  180.     }
  181. done:
  182.     if (r)
  183.         free(r);
  184.     free(buf);
  185.     return (1);
  186. bad:
  187.     if (r)
  188.         free(r);
  189.     free(buf);
  190.     return (0);
  191. }
  192.  
  193. static int
  194. cpSeparate(in, out)
  195.     IMAGE *in;
  196.     TIFF *out;
  197. {
  198.     u_char *buf = (u_char *)malloc(TIFFScanlineSize(out));
  199.     short *r = (short *)malloc(in->xsize * sizeof (short));
  200.     int x, y, z;
  201.  
  202.     for (z = 0; z < in->zsize; z++) {
  203.         for (y = in->ysize-1; y >= 0; y--) {
  204.             getrow(in, r, y, z);
  205.             for (x = 0; x < in->xsize; x++)
  206.                 buf[x] = r[x];
  207.             if (TIFFWriteScanline(out, buf, in->ysize-y-1, z) < 0)
  208.                 goto bad;
  209.         }
  210.     }
  211. done:
  212.     free(r);
  213.     free(buf);
  214.     return (1);
  215. bad:
  216.     free(r);
  217.     free(buf);
  218.     return (0);
  219. }
  220.  
  221. static void
  222. usage()
  223. {
  224.     fprintf(stderr, "usage: sgi2tiff [options] input output\n");
  225.     fprintf(stderr, "where options are:\n");
  226.     fprintf(stderr,
  227.         " -contig\tpack samples contiguously (e.g. RGBRGB...)\n");
  228.     fprintf(stderr,
  229.         " -separate\tstore samples separately (e.g. RRR...GGG...BBB...)\n");
  230.     fprintf(stderr, "\n");
  231.     fprintf(stderr,
  232.         " -lzw\t\tcompress output with Lempel-Ziv & Welch encoding\n");
  233.     fprintf(stderr,
  234.         " -packbits\tcompress output with packbits encoding\n");
  235.     fprintf(stderr,
  236.         " -none\t\tuse no compression algorithm on output\n");
  237.     fprintf(stderr, "\n");
  238.     fprintf(stderr,
  239.         " -rowsperstrip #\tmake each strip have no more than # rows\n");
  240.     exit(-1);
  241. }
  242.